package com.hzdl.cinema.dao.impl;

import com.hzdl.cinema.dao.TicketDao;
import com.hzdl.cinema.domain.Ticket;
import com.hzdl.cinema.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.math.BigDecimal;
import java.util.List;

public class TicketDaoImpl implements TicketDao {

    JdbcTemplate jdbcTemplate = new JdbcTemplate(JDBCUtils.getDataSource());

    @Override
    public List<Ticket> findMyTicket(int userId, int index, int rows) {
        String sql = "select * from ticket where user_Id=? limit ?,?";
        List<Ticket> tickets = null;
        try {
            tickets = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Ticket.class), userId, index, rows);
        } catch (Exception e) {
//            e.printStackTrace();
        }
        return tickets;
    }

    @Override
    public void returnTicket(int ticketId) {
        String sql = "update ticket set user_Id = 0 ,ticket_status =0  where ticket_Id=?";
        jdbcTemplate.update(sql, ticketId);
    }

    //总票数
    @Override
    public int findCountByUserId(int userId) {
        String sql = "select count(*) from ticket where user_Id=?";
        int totalCount = jdbcTemplate.queryForObject(sql, Integer.class, userId);
        return totalCount;
    }

    //当前页信息
    @Override
    public List<Ticket> findPageList(int userId, int index, int rows) {
        String sql = "select * from ticket where user_Id=? limit ?,?";
        List<Ticket> ticketList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Ticket.class), userId, index, rows);
        return ticketList;
    }

    @Override
    public int totalTicke() {
        String sql = "select count(*) from ticket ";
        Integer integer = jdbcTemplate.queryForObject(sql, Integer.class);
        return integer;
    }

    @Override
    public List<Ticket> fiedTicke(int index, Integer rows) {
        String sql = "select * from ticket limit ?,? ";
        List<Ticket> ticketList = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Ticket.class), index, rows);

        return ticketList;
    }

    @Override
    public BigDecimal numTicket() {
        String sql = "select SUM(ticket_Price) from  ticket";
        BigDecimal bigDecimal = jdbcTemplate.queryForObject(sql, BigDecimal.class);
        return bigDecimal;
    }
}
